﻿using GameFramework;
using GameFramework.DataTable;
using GameFramework.UI;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

public static class UIExtension
{
    /// <summary>
    /// 界面淡入淡出
    /// </summary>
    /// <param name="alpha">目标透明度</param>
    /// <param name="duration">持续时间</param>
    public static IEnumerator FadeToAlpha(this CanvasGroup canvasGroup, float alpha, float duration)
    {
        float time = 0f;
        float originalAlpha = canvasGroup.alpha;
        while (time < duration)
        {
            time += Time.deltaTime;
            canvasGroup.alpha = Mathf.Lerp(originalAlpha, alpha, time / duration);
            yield return new WaitForEndOfFrame();
        }

        canvasGroup.alpha = alpha;
    }

    /// <summary>
    /// 滑动条值平滑变化
    /// </summary>
    /// <param name="value">目标值</param>
    /// <param name="duration">持续时间</param>
    public static IEnumerator SmoothValue(this Slider slider, float value, float duration)
    {
        float time = 0f;
        float originalValue = slider.value;
        while (time < duration)
        {
            time += Time.deltaTime;
            slider.value = Mathf.Lerp(originalValue, value, time / duration);
            yield return new WaitForEndOfFrame();
        }

        slider.value = value;
    }

    /// <summary>
    /// 界面组中是否存在界面
    /// </summary>
    /// <param name="uiFormId">界面编号</param>
    public static bool HasUIForm(this UIComponent uiComponent, UIFormId uiFormId, string uiGroupName = null)
    {
        return uiComponent.HasUIForm((int)uiFormId, uiGroupName);
    }

    /// <summary>
    /// 界面组中是否存在界面
    /// </summary>
    /// <param name="uiFormId">界面编号</param>
    /// <param name="uiGroupName">界面组名称</param>
    public static bool HasUIForm(this UIComponent uiComponent, int uiFormId, string uiGroupName = null)
    {
        //IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
        //DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
        //if (drUIForm == null)
        //{
        //    return false;
        //}

        //string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
        //if (string.IsNullOrEmpty(uiGroupName))
        //{
        //    return uiComponent.HasUIForm(assetName);
        //}

        //IUIGroup uiGroup = uiComponent.GetUIGroup(uiGroupName);
        //if (uiGroup == null)
        //{
        //    return false;
        //}

        //return uiGroup.HasUIForm(assetName);

        //TODO
        return false;
    }

    /// <summary>
    /// 获取界面
    /// </summary>
    /// <param name="uiFormId">界面编号</param>
    /// <param name="uiGroupName">界面组名称</param>
    public static CustomUIForm GetUIForm(this UIComponent uiComponent, UIFormId uiFormId, string uiGroupName = null)
    {
        return uiComponent.GetUIForm((int)uiFormId, uiGroupName);
    }

    /// <summary>
    /// 获取界面
    /// </summary>
    /// <param name="uiFormId">界面编号</param>
    /// <param name="uiGroupName">界面组名称</param>
    public static CustomUIForm GetUIForm(this UIComponent uiComponent, int uiFormId, string uiGroupName = null)
    {
        //IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
        //DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
        //if (drUIForm == null)
        //{
        //    return null;
        //}

        //string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);
        //UIForm uiForm = null;
        //if (string.IsNullOrEmpty(uiGroupName))
        //{
        //    uiForm = uiComponent.GetUIForm(assetName);
        //    if (uiForm == null)
        //    {
        //        return null;
        //    }

        //    return (UGuiForm)uiForm.Logic;
        //}

        //IUIGroup uiGroup = uiComponent.GetUIGroup(uiGroupName);
        //if (uiGroup == null)
        //{
        //    return null;
        //}

        //uiForm = (UIForm)uiGroup.GetUIForm(assetName);
        //if (uiForm == null)
        //{
        //    return null;
        //}

        //return (UGuiForm)uiForm.Logic;

        //TODO
        return null;
    }

    /// <summary>
    /// 关闭界面
    /// </summary>
    /// <param name="uiForm">要关闭的界面</param>
    public static void CloseUIForm(this UIComponent uiComponent, CustomUIForm uiForm)
    {
        uiComponent.CloseUIForm(uiForm.UIForm);
    }

    /// <summary>
    /// 关闭界面
    /// </summary>
    /// <param name="uiFormId">界面编号</param>
    /// <param name="userData">用户自定义数据</param>
    public static int? OpenUIForm(this UIComponent uiComponent, UIFormId uiFormId, object userData = null)
    {
        return uiComponent.OpenUIForm((int)uiFormId, userData);
    }

    /// <summary>
    /// 打开界面
    /// </summary>
    /// <param name="uiFormId">界面编号</param>
    /// <param name="userData">用户自定义数据</param>
    public static int? OpenUIForm(this UIComponent uiComponent, int uiFormId, object userData = null)
    {
        //IDataTable<DRUIForm> dtUIForm = GameEntry.DataTable.GetDataTable<DRUIForm>();
        //DRUIForm drUIForm = dtUIForm.GetDataRow(uiFormId);
        //if (drUIForm == null)
        //{
        //    Log.Warning("Can not load UI form '{0}' from data table.", uiFormId.ToString());
        //    return null;
        //}

        //string assetName = AssetUtility.GetUIFormAsset(drUIForm.AssetName);

        //if (!drUIForm.AllowMultiInstance)
        //{
        //    if (uiComponent.IsLoadingUIForm(assetName))
        //    {
        //        return null;
        //    }

        //    if (uiComponent.HasUIForm(assetName))
        //    {
        //        return null;
        //    }
        //}

        //return uiComponent.OpenUIForm(assetName, drUIForm.UIGroupName, drUIForm.PauseCoveredUIForm, userData);

        return -1;
    }

    /// <summary>
    /// 打开UI界面
    /// </summary>
    /// <param name="uiComponent"></param>
    /// <param name="formName"></param>
    /// <param name="userData"></param>
    public static void OpenUIForm(this UIComponent uiComponent, string formName, object userData = null)
    {
        string assetName = AssetUtility.GetUIFormAsset(formName);
        uiComponent.OpenUIForm(assetName, "CustomUIForm", userData);
    }
}
